package cn.edu.swufe.stu2025.rateexample;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class ExchangeRateAdapter extends RecyclerView.Adapter<ExchangeRateAdapter.ViewHolder> {
    private List<ExchangeRate> exchangeRates;
    public ExchangeRateAdapter(List<ExchangeRate> exchangeRates) {
        this.exchangeRates = exchangeRates;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(android.R.layout.simple_list_item_2, parent, false);
        return new ViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ExchangeRate rate = exchangeRates.get(position);
        String formattedText = rate.getCurrency() + " => " + rate.getRate();
        holder.currencyTextView.setText(formattedText);
        holder.rateTextView.setText("");

        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(v.getContext(), RateCalculatorActivity.class);
            intent.putExtra("exchangeRate", rate.getRate());
            intent.putExtra("initText",rate.getCurrency() );
            v.getContext().startActivity(intent);
        });
    }
    @Override
    public int getItemCount() {
        return exchangeRates.size();
    }
    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView currencyTextView;
        TextView rateTextView;

        ViewHolder(View itemView) {
            super(itemView);
            currencyTextView = itemView.findViewById(android.R.id.text1);
            rateTextView = itemView.findViewById(android.R.id.text2);
        }
    }
}